<?php
    require_once(dirname(__FILE__)."/../include/main.php");
    require_once(dirname(__FILE__)."/../include/db.php");

    if (isset($_GET['picId'])) {
        $picId = $_GET['picId'];
        $db = connect();

        $tag_rows = $db->query("SELECT tagid, tagname FROM tags WHERE picid = ".$picId." ORDER BY tagname ASC");
        if (!$tag_rows) die("Could not load Tags from Database (Picture #".$picId."): ".$db->error);
        
        header('Content-Type: application/json');
        echo "[";
        while ($tag = $tag_rows->fetch_array()) {
            if (!$tag['tagname']) continue;
            echo "{ \"id\": ".$tag['tagid'].", \"name\": \"".htmlentities($tag['tagname'])."\" },";
        }
        echo "null]";

        mysqli_close($db);

    }
?>
